package com.shunli.cterminal.strategy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.examples.core.enums.YesOrNoEnum;
import com.shunli.cterminal.enums.ConditionTypeEnum;
import com.shunli.cterminal.model.dto.TrafficPolicyStrategyDTO;
import com.shunli.cterminal.model.res.SystemTrafficPolicyVO;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 媒体
 * </p>
 *
 * @author daodao
 * @since 2025-04-29 15:34
 */
public class PlatformStrategy implements Strategy {

    /**
     * 获取条件类型  ConditionTypeEnum
     *
     * @return
     */
    @Override
    public String getConditionTypeEnum() {
        return ConditionTypeEnum.OR.getCode();
    }

    /**
     * 执行策略 结果
     *
     * @param beforeResult 上一个规则过滤后的结果
     * @return String
     */
    @Override
    public Set<SystemTrafficPolicyVO> parse(Set<SystemTrafficPolicyVO> beforeResult, TrafficPolicyStrategyDTO trafficPolicyStrategyDTO) {
        if (CollectionUtil.isEmpty(beforeResult)) {
            return null;
        }
        beforeResult = beforeResult.stream()
                .filter(systemTrafficPolicyVO -> {
                    if (ObjectUtil.isNull(systemTrafficPolicyVO.getPlatformJsonDTO())) {
                        return false;
                    }
                    // 全部
                    if (YesOrNoEnum.YES.getCode().equals(systemTrafficPolicyVO.getPlatformJsonDTO().getAllEnable())) {
                        return true;
                    } else {
                        // 是包含关系还是非包含关系
                        if (YesOrNoEnum.YES.getCode().equals(systemTrafficPolicyVO.getPlatformJsonDTO().getContainEnable())) {
                            return systemTrafficPolicyVO.getPlatformJsonDTO().getIdList().contains(trafficPolicyStrategyDTO.getPlatform());
                        } else if (YesOrNoEnum.NO.getCode().equals(systemTrafficPolicyVO.getPlatformJsonDTO().getContainEnable())) {
                            return !systemTrafficPolicyVO.getPlatformJsonDTO().getIdList().contains(trafficPolicyStrategyDTO.getPlatform());
                        } else {
                            return false;
                        }
                    }
                })
                .collect(Collectors.toSet());
        return beforeResult;
    }
}
